"""
  Copyright (c) 2024 Northeastern University (China)
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
"""
# 集成识别和自动化验证，以及生成可视化需要的数据

import os

import click

from src.common_utils.utils_file import write_line_jl
from src.component_identification.automated_verification import Verification
from src.component_identification.component_detector import Detector


def components(target_src_path,theta,beta,cpu):
    """检测目标代码组成成分"""
    analysis_detect=None
    detector = Detector(theta, beta)
    resDict, fileCnt, funcCnt, lineCnt = detector.hashing(target_src_path)
    if len(resDict) != 0:
        components, mid_res = detector.detector_main(resDict, cpu)
        if components is not None:
            components = list(components)  # 将multiprocessing.managers.ListProxy转换成正常list
            # 自动化验证
            verification = Verification()
            analysis_detect = verification.analysis_common(components, target_src_path)  # 这就得到了可视化中的节点列表
    return analysis_detect
@click.command()
@click.option('--target_src_path', type=str, required=True, help='Path to target source')
@click.option('--component_output_path', type=str, default=None, help='Path to component output')
@click.option('--theta', type=float, default=0.7, help='Description for theta parameter')
@click.option('--beta', type=float, default=0.6, help='Description for theta parameter')
@click.option('--cpu', type=int, default=8, help='Number of CPU cores')
def main(target_src_path, component_output_path, theta, beta, cpu):
    """
    对外暴露接口
    @param target_src_path:
    @param component_output_path:
    @param theta:
    @param beta:
    @param cpu:
    @return:
    """
    analysis_detect=components(target_src_path,theta,beta,cpu)
    target_src_repo = os.path.basename(target_src_path)
    if component_output_path is not None:
        write_line_jl(os.path.join(component_output_path, target_src_repo + '_components.json'),analysis_detect)
    print(f"components={analysis_detect}")


if __name__ == "__main__":
    main()


